<?php

Class UserAccountModel extends Model {

    protected $Log;

    public function _initialize() {
        parent::_initialize();
        $this->Log = M('UserAccountLog');
    }

    /**
     * 初始化用户帐号
     * @param type $uid
     * @return type 
     */
    public function initAccount($uid) {
        $userAccount = $this->find($uid);
        if (!$userAccount) {
            $userAccount = array(
                'user_id'     => $uid,
                'score'       => 0,
                'change_time' => time(),
            );
            $this->add($userAccount);
        }
        return $userAccount;
    }

    /**
     * 计算花费的金币
     * @param type $param
     * @return type 
     */
    public function countSum($param) {
        $total = 0;
        //图片放大
        if ($param['zoom'] == 'checked' || $param['img_zoom'] == 1) {
            $total += C('ZOOM_PAY_SCORE');
        }
        //链接dz
        if ($param['link_status'] == 'checked' || $param['link_status'] == 1) {
            $param['link'] = $param['link_days'] ? $param['link_days'] : $param['link'];
            $total += C('LINK_DAY_PAY') * (int) $param['link'];
        }
        //次级页面
        if ($param['page_status'] == 'checked' || $param['page_status'] == 1) {
            $param['page'] = $param['page_days'] ? $param['page_days'] : $param['page'];
            $total += C('PAGE_DAY_PAY') * (int) $param['page'];
        }
        //发布天数
        if ($param['days_status'] == 1) {
            $total += C('DAY_PAY') * ((int) $param['days'] - 1);
        }
        //@todo 首页置顶
        if ($param['index_top_status']) {
            $total += C('INDEX_TOP_PAY') * (int) $param['index_top_days'];
        }
        //@todo 分类置顶
        if ($param['cat_top_status']) {
            $total += C('CAT_TOP_PAY') * (int) $param['cat_top_days'];
        }

        return $total;
    }

    /**
     * 用户金币变更操作
     * @param type $uid
     * @param type $score
     * @param type $action
     * @param type $msg
     * @return type 
     */
    public function chargeUserAccount($uid, $score, $action = 'SYSTEM', $msg = "系统操作") {
        $userAccount = $this->initAccount($uid);
        //开启事务
        M()->startTrans();
        $userAccount['score'] += $score;
        if ($userAccount['score'] < 0) {
            return reStatus(false, '金币不足');
        }
        $userAccount['change_time'] = time();
        $chargeRes                  = $this->save($userAccount);
        $Log                        = array(
            'user_id'     => $uid,
            'action'      => $action,
            'score'       => $score,
            'msg'         => $msg,
            'create_time' => time()
        );
        $logRes       = $this->Log->add($Log);
        if ($chargeRes && $logRes) {
            M()->commit();
            return reStatus(true, '积分变更成功', $userAccount);
        } else {
            M()->rollback();
            return reStatus(false, '积分变更失败');
        }
    }

}

?>
